package com.fangfei.runtest.model;

import com.alibaba.fastjson.JSON;
import com.fangfei.runtest.Config.TestConfig;

import java.util.*;

public class TestFunctionModel {

    public String getphone(){
        long phone = 13800000000L;
        Random rand = new Random();
        int num = rand.nextInt(10000000) + 1;
        return String.valueOf(phone+num);
    }

    public String getnickname(){
        long phone = 800000000L;
        Random rand = new Random();
        int num = rand.nextInt(10000000) + 1;
        return String.valueOf(phone+num);
    }

    public String getcontent(){
        long phone = 700000000L;
        Random rand = new Random();
        int num = rand.nextInt(10000000) + 1;
        return String.valueOf(phone+num);
    }

    public String getmedia(){
        long phone = 600000000L;
        Random rand = new Random();
        int num = rand.nextInt(10000000) + 1;
        return String.valueOf(phone+num);
    }

    public List getuser(){

        return Arrays.asList(402,403);
    }

    public String setcode(String jsonarr, TestConfig testConfig) throws Exception{
        Map mapData = JSON.parseObject(jsonarr, Map.class);
        String code = String.valueOf(mapData.get("code"));
        if (!"1000".equals(code)){
            throw new Exception("操作错误了，没有code");
        }

        List rows = testConfig.getJdbcTemplate().queryForList("select code,phone,type from message where phone=? order by id desc limit 0,1", testConfig.getUserMap().get("phone"));
        Iterator it = rows.iterator();
        while(it.hasNext()) {
            Map urlMap = (Map)it.next();
            String rcode = (String) urlMap.get("code");
            if (!rcode.isEmpty()){
                return  rcode;
            }
        }
        return "";
    }

    public String settoken(String jsonarr, TestConfig testConfig) throws Exception{
        Map mapData = JSON.parseObject(jsonarr, Map.class);
        String code = String.valueOf(mapData.get("code"));
        if (!"1000".equals(code)){
            throw new Exception("操作错误了，没有code");
        }

        Map tokendata = (Map) mapData.get("data");
        String rtoken = (String) tokendata.get("token");
        return rtoken;
    }
}
